// pages/goods/index.js
var http = require("../../utils/http.js")

import api from "../../utils/api.js";
import common from '../../utils/common';
import message from "../../utils/message.js";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    categoryList: [
    ],
    active_index:3,
    productList:[],
    category_product_list:[],
    static_url:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var me = this;

    /*
    http.doGet("/product/getList" , {} , function( resp ){
      console.log(resp.data);
      var result = resp.data;
      if(result.status==1){
        me.setData({
          productList:result.data
        })

      }else{

        

      }

    })

    */

    /*
    api.getProductList((result) => {
      console.log(result);

      this.setData({
        categoryList:result.data,
        //productList:result.productList
      })
    })

    */

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    common.updateCartNums();

    this.getCategoryProductList();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getCategoryProductList:function(  ){

    message.show_loading();
    api.product.getCategoryProductList( (result)=>{
      message.hide_loading();
      this.setData({
        category_product_list:result.data
      })

      this.data.category_product_list.map( (item , index)=>{
        wx.createSelectorQuery().select("#item_"+item.id).boundingClientRect( function(rect){
          item.offsetTop = rect.top;
          item.height = rect.height;
        } ).exec();

      } )

    })

  },
  imageLoadError:function(e){
    //图片加载失败时，用默认图片替代
    var cid = e.currentTarget.dataset.cid;
    var index = e.currentTarget.dataset.index;


    if(e.type=='error'){
      var product_list = this.data.category_product_list;


      product_list[cid].products[index].product_thumb="../../images/product-error.png";

      this.setData({
        category_product_list:product_list
      })
    }


  },
  selectCategory:function( e ){
    var id = e.currentTarget.dataset.id
    this.setData({
      active_index:id,
      toView:"item_"+id
    })
  },

  doScrollHandler:function(event){
    //右侧产品滚动触发事件
    var current_scroll_top = event.detail.scrollTop;
    //console.log(current_scroll_top)


    this.data.category_product_list.map((item , index)=>{
      //console.log(item.height);
      if(current_scroll_top > item.offsetTop && current_scroll_top< item.offsetTop+item.height){
          this.setData({
            active_index:  item.id,
          })
      }
    })


  },
  showDetail:function( e ){
    console.log("加载详情")
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods/detail_custom?id='+id,
      success:function(){

      }
    })
  },
  doAddGood:function(e){

    
    var id = e.currentTarget.dataset.id

    var index = e.currentTarget.dataset.index;
    var parent_index = e.currentTarget.dataset.parentindex;

    
    console.log(parent_index)

    var t = this.data.category_product_list[parent_index].products[index];
    console.log(t);

    

    var s = parseInt(t.nums);
    s++;

    t.nums = s;

    this.setData({
      category_product_list: this.data.category_product_list
    });

    
    
    console.log("添加商品")

  },
  doReduceGood:function(e){
    var id = e.currentTarget.dataset.id
    console.log("删除商品")


    var id = e.currentTarget.dataset.id

    var index = e.currentTarget.dataset.index;
    var parent_index = e.currentTarget.dataset.parentindex;


    var t = this.data.category_product_list[parent_index].products[index];
    console.log(t);

    

    var s = parseInt(t.nums);
    s--;

    if(s<0){
      s=0;
    }

    t.nums = s;

    this.setData({
      category_product_list: this.data.category_product_list
    });

  }
})